package com.fosun.spr.core.utils;

import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;

import com.fosun.spr.core.base.CoBaseService;
import com.fosun.spr.core.exception.ServiceException;

public class SprDaoUtils {
	
	public static void assertExistsById(Object mapping,Object id){
		Object record = null;
		try {
			Method m = mapping.getClass().getMethod("selectByPrimaryKey", id.getClass());
			record = m.invoke(mapping, id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
		ExtAsserts.dbRowExistsByIsDeleted(record, id);
	}
	
	public static Object getById(Object mapping,Object id){
		Object record = null;
		try {
			Method m = mapping.getClass().getMethod("selectByPrimaryKey", id.getClass());
			record = m.invoke(mapping, id);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
		ExtAsserts.dbRowExistsByIsDeleted(record, id);
		return record;
	}
	
	
	public static void delete(Object mapping,Object id){
		Object record = getById(mapping,id);
		try {
			Method m = record.getClass().getMethod("setIsDeleted", String.class);
			m.invoke(record, CoBaseService.IS_DELETED_Y);
			
			Method m2 = record.getClass().getMethod("setRecUpdateTime", Date.class);
			m2.invoke(record, new Date());

			Method m3 = mapping.getClass().getMethod("updateByPrimaryKey", record.getClass());
			m3.invoke(mapping, record);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
	}

	public static void updateByPrimaryKey(Object mapping,Object record){
		try {		
			Method m2 = record.getClass().getMethod("setRecUpdateTime", Date.class);
			m2.invoke(record, new Date());

			Method m3 = mapping.getClass().getMethod("updateByPrimaryKey", record.getClass());
			m3.invoke(mapping, record);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
	}
	
	public static void insert(Object mapping,Object record){
		try {		
			Method m0 = record.getClass().getMethod("setId", String.class);
			m0.invoke(record, CoBaseService.getUuid());
			
			Method m = record.getClass().getMethod("setIsDeleted", String.class);
			m.invoke(record, CoBaseService.IS_DELETED_N);
			
			Method m2 = record.getClass().getMethod("setRecCreateTime", Date.class);
			m2.invoke(record, new Date());

			Method m3 = mapping.getClass().getMethod("insertSelective", record.getClass());
			m3.invoke(mapping, record);
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}
	}
	/**
	 * selectByExample的返回单条记录
	 * 
	 * @param mapping
	 * @param example
	 * @return
	 */
	public static Object getRowByExample(Object mapping,Object example){
		try {		
			
			Method m = mapping.getClass().getMethod("selectByExample", example.getClass());
			List list = (List)m.invoke(mapping, example);
			if(list.size()>0){
				return list.get(0);
			}
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e.getMessage());
		}	
		return null;
	}
}
